﻿using System;
using System.Collections.Generic;
using System.Text;
using SynoManager.AudioStation.Data;

namespace SynoManager.AudioStation
{
    public class AudioStationService : ServiceBase
    {
        internal AudioStationClient client;

        public override void Initialize(SynoDevice device)
        {
            base.Initialize(device);

            client = new AudioStationClient(device);
        }

        private AudioStationGuiPlugin plugin;
        public AudioStationGuiPlugin Plugin
        {
            get { return plugin; }
            set { plugin = value; }
        }

        public Playlist[] GetPlaylists()
        {
            List<Playlist> list = new List<Playlist>();
            foreach(AudioStationClient.PlaylistInfo info in client.GetPlaylists())
            {
                list.Add(new Playlist(this, info));
            }
            return list.ToArray();
        }

        public Playlist CreatePlaylist(string name, MusicFile[] files)
        {
            List<int> ids = new List<int>();
            foreach(MusicFile file in files)
                ids.Add(file.ID);
            var result = client.CreatePlaylist(name, ids.ToArray());

            return new Playlist(this, result.data);
        }

        public MusicFile[] GetAll(int offset, int count, out int total)
        {
            return Find(
                MusicFindField.All, String.Empty, 
                MusicSorting.Artist, SortDirection.Ascending, 
                offset, count,
                out total);
        }

        public MusicFile[] Find(
            MusicFindField field, string keyword, 
            MusicSorting sorting, SortDirection direction, 
            int offset, int count, 
            out int total)
        {
            string directionCode;
            switch (direction)
            {
                case SortDirection.Ascending:
                    directionCode = "asc";
                    break;
                case SortDirection.Descending:
                    directionCode = "desc";
                    break;
                default:
                    throw new ArgumentOutOfRangeException("direction");
            }

            AudioStationClient.LibraryContents contents = client.GetMusic(
                field.ToString().ToLower(), keyword,
                sorting.ToString().ToLower(),
                directionCode,
                offset, count);

            total = contents.total;

            List<MusicFile> files = new List<MusicFile>();
            foreach(var file in contents.items)
            {
                files.Add(new MusicFile(this, file));
            }
            return files.ToArray();
        }
    }

    public enum MusicFindField
    {
        All,
        Artist,
        Title,
        Album,
        Genre,
    }

    public enum MusicSorting
    {
        Title,
        Artist,
        Album,
        Genre,
        Duration,
    }

    public enum SortDirection
    {
        Ascending,
        Descending
    }
}
